package routes

import (
	v1 "ginblog/api/v1"
	"ginblog/middleware"
	"ginblog/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

// InitRouter 全局路由设置
func InitRouter() {
	gin.SetMode(utils.AppMode)
	// router := gin.Default()
	router := gin.New()
	router.Use(gin.Recovery(), middleware.Logger(), middleware.Cors())

	router.LoadHTMLGlob("static/admin/templates/*")
	router.Static("static", "static/admin/static")
	router.StaticFile("favicon.ico", "static/admin/favicon.ico")

	router.GET("/admin", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})
	router.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "blog.html", nil)
	})

	authRouterV1 := router.Group("api/v1")
	authRouterV1.Use(middleware.JwtToken())
	{
		// 用户模块的路由接口
		authRouterV1.PUT("user/:id", v1.EditUser)
		authRouterV1.DELETE("user/:id", v1.DeleteUser)

		// 分类模块的路由接口
		authRouterV1.POST("category/add", v1.AddCategory)
		authRouterV1.PUT("category/:id", v1.EditCategory)
		authRouterV1.DELETE("category/:id", v1.DeleteCategory)

		// 文章模块的路由接口
		authRouterV1.POST("article/add", v1.AddArticle)

		authRouterV1.PUT("article/:id", v1.EditArticle)
		authRouterV1.DELETE("article/:id", v1.DeleteArticle)

		// 获取cos临时密钥
		authRouterV1.GET("cos/credential", v1.GetTempCredential)

		// 更新个人设置
		authRouterV1.PUT("profile/:id", v1.UpdateProfile)
	}
	pubRouterV1 := router.Group("api/v1")
	{
		pubRouterV1.POST("user/add", v1.AddUser)
		pubRouterV1.GET("users", v1.GetUsers)
		pubRouterV1.GET("user/:id", v1.GetUserInfo)
		pubRouterV1.POST("login", v1.Login)

		pubRouterV1.GET("category/:id", v1.GetCategory)
		pubRouterV1.GET("categorys", v1.GetCategorys)

		pubRouterV1.GET("articles", v1.GetArticles)
		pubRouterV1.GET("article/list/:cid", v1.GetArticlesOfCategory)
		pubRouterV1.GET("article/info/:id", v1.GetArticleInfo)
		pubRouterV1.GET("profile/:id", v1.GetProfile)
	}

	router.Run(utils.HttpPort)
}
